package cn.stylefeng.guns.modular.deduct.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.deduct.entity.Deduct;
import cn.stylefeng.guns.modular.deduct.mapper.DeductMapper;
import cn.stylefeng.guns.modular.deduct.model.params.DeductParam;
import cn.stylefeng.guns.modular.deduct.model.result.DeductResult;
import  cn.stylefeng.guns.modular.deduct.service.DeductService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 扣费记录表 服务实现类
 * </p>
 *
 * @author liuqiang
 * @since 2020-02-09
 */
@Service
public class DeductServiceImpl extends ServiceImpl<DeductMapper, Deduct> implements DeductService {

    @Override
    public void add(DeductParam param){
        Deduct entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(DeductParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(DeductParam param){
        Deduct oldEntity = getOldEntity(param);
        Deduct newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public DeductResult findBySpec(DeductParam param){
        return null;
    }

    @Override
    public List<DeductResult> findListBySpec(DeductParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(DeductParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public BigDecimal sumConsumeAmount(DeductParam param) {
        return this.baseMapper.sumConsumeAmount(param);
    }

    private Serializable getKey(DeductParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private Deduct getOldEntity(DeductParam param) {
        return this.getById(getKey(param));
    }

    private Deduct getEntity(DeductParam param) {
        Deduct entity = new Deduct();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
